/**
  ******************************************************************************
  * @file           : usart_bsp.cpp
  * @author         : Tessia
  * @brief          : None
  * @attention      : None
  * @date           : 2024/3/1
  ******************************************************************************
  */



#include "usart_bsp.h"

extern uint32_t uart_err_count;
extern twins_receive_info twins_receive;
extern twins_receive_info_tran twins_receive_tran;
extern uint8_t enemy_aim_enable[12];
extern float chassis_yaw_offset;
extern uint8_t mini_camera_0_enemy_data[12];

uint8_t is_huart6_lost = 0;
uint32_t huart6_cnt = 0;
uint32_t radar_offset_cnt = 0;
float yaw_compensation = 0;
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
    if(huart->Instance == USART1){ //与右头的视觉通信
        HAL_UARTEx_ReceiveToIdle_DMA(&huart1, usb_tx_buf_right, sizeof(usb_tx_buf_right));
        __HAL_DMA_DISABLE_IT(huart1.hdmarx, DMA_IT_HT);
        uart_err_count = 0;
        return;
    }else if(huart->Instance == USART6) { //与右头的其他信息交互
        if(twins_receive_tran.frame_header == 0x30 && twins_receive_tran.frame_tail == 0x31) {
            huart6_cnt = 0;
            radar_offset_cnt ++;
            twins_receive.twins_pitch = twins_receive_tran.twins_pitch;
            twins_receive.twins_yaw = twins_receive_tran.twins_yaw;
            twins_receive.radar_total_yaw = twins_receive_tran.radar_total_yaw;
            twins_receive.is_target_detected = twins_receive_tran.is_target_detected;
            twins_receive.auto_aim = twins_receive_tran.auto_aim;
            twins_receive.is_left_of_axis = twins_receive_tran.is_left_of_axis;
            twins_receive.go_attack_outpost = twins_receive_tran.go_attack_outpost;
            twins_receive.is_radar_connect = twins_receive_tran.is_radar_connect;
            twins_receive.camera_0_data = twins_receive_tran.camera_0_data;
            if(twins_receive.is_radar_connect == 1 && radar_offset_cnt > 1500){     //这里5s校准一次
                yaw_compensation = twins_receive.radar_total_yaw - (INS.My_Yaw_Origin - chassis_yaw_offset);
                radar_offset_cnt = 0;
            }

            twins_receive.hero_aim_permit = twins_receive_tran.hero_aim_permit;
            twins_receive.engineer_aim_permit = twins_receive_tran.engineer_aim_permit;
            twins_receive.infantry_3_aim_permit = twins_receive_tran.infantry_3_aim_permit;
            twins_receive.infantry_4_aim_permit = twins_receive_tran.infantry_4_aim_permit;
            twins_receive.infantry_5_aim_permit = twins_receive_tran.infantry_5_aim_permit;
            twins_receive.sentry_aim_permit = twins_receive_tran.sentry_aim_permit;
            twins_receive.base_aim_permit = twins_receive_tran.base_aim_permit;
            twins_receive.outpost_aim_permit = twins_receive_tran.outpost_aim_permit;

            enemy_aim_enable[hero] = twins_receive.hero_aim_permit;
            enemy_aim_enable[engineer] = twins_receive.engineer_aim_permit;
            enemy_aim_enable[infantry_3] = twins_receive.infantry_3_aim_permit;
            enemy_aim_enable[infantry_4] = twins_receive.infantry_4_aim_permit;
            enemy_aim_enable[infantry_5] = twins_receive.infantry_5_aim_permit;
            enemy_aim_enable[sentry] = twins_receive.sentry_aim_permit;
            enemy_aim_enable[base] = twins_receive.base_aim_permit;
            enemy_aim_enable[outpost] = twins_receive.outpost_aim_permit;

            mini_camera_0_enemy_data[hero] = twins_receive.camera_0_data >> (hero - 1) & 0x01;
            mini_camera_0_enemy_data[engineer] = twins_receive.camera_0_data >> (engineer - 1) & 0x01;
            mini_camera_0_enemy_data[infantry_3] = twins_receive.camera_0_data >> (infantry_3 - 1) & 0x01;
            mini_camera_0_enemy_data[infantry_4] = twins_receive.camera_0_data >> (infantry_4 - 1) & 0x01;
            mini_camera_0_enemy_data[infantry_5] = twins_receive.camera_0_data >> (infantry_5 - 1) & 0x01;
            mini_camera_0_enemy_data[sentry] = twins_receive.camera_0_data >> (sentry - 1) & 0x01;
            mini_camera_0_enemy_data[0] = (mini_camera_0_enemy_data[hero] && enemy_aim_enable[hero]) || (mini_camera_0_enemy_data[engineer] && enemy_aim_enable[engineer])
                                          || ((mini_camera_0_enemy_data[infantry_3]) && enemy_aim_enable[infantry_3]) || (mini_camera_0_enemy_data[infantry_4] && enemy_aim_enable[infantry_4])
                                          || (mini_camera_0_enemy_data[infantry_5] && enemy_aim_enable[infantry_5]) || (mini_camera_0_enemy_data[sentry] && enemy_aim_enable[sentry]);

            HAL_UARTEx_ReceiveToIdle_DMA(&huart6, (uint8_t *)&twins_receive_tran, sizeof(twins_receive_tran));
            __HAL_DMA_DISABLE_IT(huart6.hdmarx, DMA_IT_HT);
        }else{
            HAL_UARTEx_ReceiveToIdle_DMA(&huart6, (uint8_t *)&twins_receive_tran, sizeof(twins_receive_tran));
            __HAL_DMA_DISABLE_IT(huart6.hdmarx, DMA_IT_HT);
        }
    }
}